package com.t.bricks.bussiness.service.msgmanage.impl;

import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.db.dao.msgmanage.MsgManageDao;
import com.t.bricks.bussiness.db.entity.msgmanage.MsgManage;
import com.t.bricks.bussiness.service.msgmanage.MsgManageService;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.function.data.StringUtil;
import com.t.bricks.utils.getui.SendMsg;
import com.t.bricks.utils.spring.ModuleUtil;

/**
 * 消息管理'MsgManage表'基本业务操作类接口实现
 */
@Service
public class MsgManageServiceImpl implements MsgManageService<MsgManage> {

	@Autowired
	private MsgManageDao<MsgManage> msgManageDao;
	
	/**
	 * 是否忙碌中
	 */
	private static boolean isBus = false;

	/**
	 * App推送配置,应用程序ID
	 */
	@Value("${app.getui.id:}")
	private String getuiAppId;

	/**
	 * App推送配置,应用程序Key
	 */
	@Value("${app.getui.key:}")
	private String getuiAppKey;

	/**
	 * App推送配置,应用程序MasterSecret
	 */
	@Value("${app.getui.secret:}")
	private String getuiAppMasterSecret;
	
	/**
	 * 同步线程
	 * Spring 启动完成后，会自动调用此方法
	 */
	@PostConstruct
	private void doThread() {
		this.sendThread();
	}
	
	/**
	 * 自定义新增
	 * @param msgManage 消息管理 对象实体
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity addCustom(MsgManage msgManage) {
		if (null == msgManage) {
			return new MsgEmity(false, "请求操作的参数为空", 8000);
		}
		
		if (StringUtil.isEmpty(msgManage.getsEmail())
				&& StringUtil.isEmpty(msgManage.getsSms())
				&& StringUtil.isEmpty(msgManage.getsWeChat())
				&& StringUtil.isEmpty(msgManage.getsApp())
				&& StringUtil.isEmpty(msgManage.getsSystem())) {
			return new MsgEmity(false, "消息至少要有一个发送方式", 8301);
		}
		
		if (!StringUtil.isEmpty(msgManage.getsEmail())
				&& StringUtil.isEmail(msgManage.getsEmail())) {
			return new MsgEmity(false, "邮箱格式错误", 8302);
		}
		
		if (!StringUtil.isEmpty(msgManage.getsSms())
				&& StringUtil.isMobileNO(msgManage.getsSms())) {
			return new MsgEmity(false, "电话号码错误", 8302);
		}

		msgManage.setdEmailSend(null);//不能由前端提供
		msgManage.setiEmailError(0);//不能由前端提供
		msgManage.setdSmsSend(null);//不能由前端提供
		msgManage.setiSmsError(0);//不能由前端提供
		msgManage.setdWeChatSend(null);//不能由前端提供
		msgManage.setiWeChatError(0);//不能由前端提供
		msgManage.setdAppSend(null);//不能由前端提供
		msgManage.setiAppError(0);//不能由前端提供
		msgManage.setdSystemSend(null);//不能由前端提供
		msgManage.setiSystemError(0);//不能由前端提供
		
		MsgEmity me = this.add(msgManage);
		if (!me.isSuccess()) {
			return me;
		}
		
		this.sendThread();
		
		return me;
	}
	
	/**
	 * 线程处理发送消息
	 * @return
	 */
	private void sendThread() {
		if (isBus) {
			return;
		}
		
		isBus = true;

		MsgEmity me = msgManageDao.findByType(0, 5);
		if (!me.isSuccess()) {
			isBus = false;
			return;
		}
		
		List<MsgManage> list = me.getData();
		if (null == list || list.size() < 1) {
			isBus = false;
			return;
		}
		
		ModuleUtil.runThread(() -> {
			while(null != list && list.size() > 1) {
				this.send(list.get(0));
				list.remove(0);
			}
			
			isBus = false;
		});
		
		sendThread();//回调
	}
	
	/**
	 * 发送消息
	 * @param msgManage
	 */
	private void send(MsgManage msgManage) {
		if (null == msgManage) {
			return;
		}
		
		if (!StringUtil.isEmpty(msgManage.getsEmail())) {
			sendMail(msgManage);
		}
		
		if (!StringUtil.isEmpty(msgManage.getsSms())) {
			sendSms(msgManage);
		}
		
		if (!StringUtil.isEmpty(msgManage.getsWeChat())) {
			sendWeChat(msgManage);
		}
		
		if (!StringUtil.isEmpty(msgManage.getsApp())) {
			sendApp(msgManage);
		}
		
		if (!StringUtil.isEmpty(msgManage.getsSystem())) {
			sendSystem(msgManage);
		}
	}
	
	/**
	 * 发送邮箱
	 * @param msgManage
	 */
	private void sendMail(MsgManage msgManage) {
		Log.error("发送邮箱功能未实现");
		msgManageDao.addError(msgManage.getiId(), 1, 5);
	}
	
	/**
	 * 发送短信
	 * @param msgManage
	 */
	private void sendSms(MsgManage msgManage) {
		Log.error("发送邮箱功能未实现");
		msgManageDao.addError(msgManage.getiId(), 2, 5);
	}
	
	/**
	 * 发送微信
	 * @param msgManage
	 */
	private void sendWeChat(MsgManage msgManage) {
		Log.error("发送微信功能未实现");
		msgManageDao.addError(msgManage.getiId(), 3, 5);
	}
	
	/**
	 * App推送
	 * @param msgManage
	 */
	private void sendApp(MsgManage msgManage) {
		MsgEmity me = SendMsg.initApi(getuiAppId, getuiAppKey, getuiAppMasterSecret);
		if (!me.isSuccess()) {
			return;
		}
		
		me = SendMsg.pushByCid(msgManage.getsApp(), msgManage.getsTheme(), msgManage.getsContent(), null);
		if (!me.isSuccess()) {
			msgManageDao.addError(msgManage.getiId(), 4, null);//null累加1
			return;
		}
		
		msgManageDao.succeed(msgManage.getiId(), 4);
	}
	
	/**
	 * 发送系统消息
	 * @param msgManage
	 */
	private void sendSystem(MsgManage msgManage) {
		Log.error("发送系统消息功能未实现");
		msgManageDao.addError(msgManage.getiId(), 5, 5);
	}

}
